package com.ruoyi.sampleRecord.service.impl;

import java.util.Calendar;
import java.util.List;

import com.ruoyi.attachedData.domain.AttachedData;
import com.ruoyi.attachedRecord.domain.AttachedRecord;
import com.ruoyi.commission.domain.Commission;
import com.ruoyi.commission.service.ICommissionService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.engineering.domain.Engineering;
import com.ruoyi.engineering.service.IEngineeringService;
import com.ruoyi.sample.domain.Sample;
import com.ruoyi.sample.service.ISampleService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sampleRecord.mapper.SampleRecordMapper;
import com.ruoyi.sampleRecord.domain.SampleRecord;
import com.ruoyi.sampleRecord.service.ISampleRecordService;

/**
 * 领样记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@Service
public class SampleRecordServiceImpl implements ISampleRecordService
{
    @Autowired
    private SampleRecordMapper sampleRecordMapper;

    @Autowired
    private ISampleService sampleService;

    @Autowired
    private ICommissionService commissionService;

    @Autowired
    private IEngineeringService engineeringService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISampleRecordService sampleRecordService;

    /**
     * 查询领样记录
     *
     * @param id 领样记录主键
     * @return 领样记录
     */
    @Override
    public SampleRecord selectSampleRecordById(Long id)
    {
        return sampleRecordMapper.selectSampleRecordById(id);
    }

    public Integer  selectSampleRecordForSum(SampleRecord sampleRecord){
        return sampleRecordMapper.selectSampleRecordForSum(sampleRecord);
    }

    /**
     * 查询领样记录列表
     *
     * @param sampleRecord 领样记录
     * @return 领样记录
     */
    @Override
    public List<SampleRecord> selectSampleRecordList(SampleRecord sampleRecord)
    {
        return sampleRecordMapper.selectSampleRecordList(sampleRecord);
    }

    /**
     * 新增领样记录
     *
     * @param sampleRecord 领样记录
     * @return 结果
     */
    @Override
    public int insertSampleRecord(SampleRecord sampleRecord)
    {
        sampleRecord.setCreateTime(DateUtils.getNowDate());
        Integer isSr = sampleRecordMapper.insertSampleRecord(sampleRecord);

        //原始编号
        Long id = sampleRecord.getId();
        Sample sampleOne = sampleService.selectSampleById(sampleRecord.getSampleId());

        //生成报告编号  工程编号➕JL➕分类编码➕年份后两位➕序号
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR); // 获取当前年份

        Long commissionId = sampleRecord.getCommissionId();
        Commission commissionOne = commissionService.selectCommissionById(commissionId);

        Engineering engineeringOne = engineeringService.selectEngineeringById(commissionOne.getEngineeringId());

        //分类编码
        String fenleiNumber = "";
        List<SysDictData> sysDictDatalist = dictDataService.selectDictDataByDictType("detection_item_indoor");
        for (SysDictData sysDictData : sysDictDatalist) {
            if (sysDictData.getDictValue().equals(sampleOne.getProject())) {
                fenleiNumber = sysDictData.getCssClass();
            }
        }

        String year2 = String.valueOf(year);
        String lastTwoChars = year2.substring(year2.length() - 2);
        String id_str = "";
        if(id < 10L){
            id_str = "00"+String.valueOf(id);
        }else if(id < 100L && id >= 10L){
            id_str = "0"+String.valueOf(id);
        }else {
            id_str = String.valueOf(id);
        }
        String number = engineeringOne.getEngineeringNumber()+"JL"+ fenleiNumber + lastTwoChars + id_str;

        SampleRecord sampleRecord2 = new SampleRecord();
        sampleRecord2.setId(id);
        sampleRecord2.setOriginalNumber(number);
        sampleRecordService.updateSampleRecord(sampleRecord2);


        //录入数据的数量 大于等于 试验项目的数量 就改变状态
        SampleRecord sampleRecord3 = new SampleRecord();
        sampleRecord3.setSampleId(sampleRecord.getSampleId());
        List<SampleRecord> sampleRecordList = sampleRecordService.selectSampleRecordList(sampleRecord3);

        Integer allSampleRecorNum = 0;
        for (SampleRecord sampleRecord4 : sampleRecordList) {
            Integer sampleNumber = Integer.valueOf(sampleRecord4.getSampleNumber());
            allSampleRecorNum+=sampleNumber;
        }
        Integer allNumber = Integer.valueOf(sampleOne.getNumber());
        if(allSampleRecorNum >= allNumber){
            Sample sample = new Sample();
            sample.setId(sampleRecord.getSampleId());
            sample.setIsDone(2L);
            sampleService.updateSample(sample);
        }

        return isSr;
    }

    /**
     * 修改领样记录
     *
     * @param sampleRecord 领样记录
     * @return 结果
     */
    @Override
    public int updateSampleRecord(SampleRecord sampleRecord)
    {
        sampleRecord.setUpdateTime(DateUtils.getNowDate());
        return sampleRecordMapper.updateSampleRecord(sampleRecord);
    }

    /**
     * 批量删除领样记录
     *
     * @param ids 需要删除的领样记录主键
     * @return 结果
     */
    @Override
    public int deleteSampleRecordByIds(Long[] ids)
    {
        return sampleRecordMapper.deleteSampleRecordByIds(ids);
    }


    /**
     * 批量逻辑删除领样记录
     *
     * @param ids 需要删除的领样记录主键
     * @return 结果
     */
    @Override
    public int logicDeleteSampleRecord(Long[] ids,String delType,Long delUser)
    {
        return sampleRecordMapper.logicDeleteSampleRecord(ids,delType,delUser);
    }

    /**
     * 删除领样记录信息
     *
     * @param id 领样记录主键
     * @return 结果
     */
    @Override
    public int deleteSampleRecordById(Long id)
    {
        return sampleRecordMapper.deleteSampleRecordById(id);
    }
}
